import Vue from 'vue'
import VueRouter from 'vue-router'
// 登录
import Login from '.././components/Login.vue'
import Home from '.././components/Home.vue'
import Welcome from '.././components/Welcome.vue'
// 用户列表
import Users from '.././components/user/Users.vue'
// 权限列表
import Rights from '.././components/power/Rights.vue'
// 角色列表
import Roles from '.././components/power/Roles.vue'
// 商品分类
import Cate from '.././components/goods/Cate.vue'
// 分类参数
import Params from '.././components/goods/Params.vue'
// 商品列表
import GoodsList from '.././components/goods/List.vue'
// 商品列表中的添加商品页面
import Add from '../components/goods/Add.vue'
// 订单列表
import Order from '../components/order/Order.vue'
import Report from '../components/report/Report.vue'


Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    redirect: '/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/home',
    component: Home,
    // 进入home页面，重定向直接跳转到子组件welcome页面
    redirect: '/welcome',
    children: [
      { path: '/welcome', component: Welcome },
      { path: '/users', component: Users },
      { path: '/rights', component: Rights },
      { path: '/Roles', component: Roles },
      { path: '/categories', component: Cate },
      { path: '/params', component: Params },
      { path: '/goods', component: GoodsList },
      { path: '/goods/add', component: Add },
      { path: '/orders', component: Order },
      { path: '/reports', component: Report }
    ]
  }
]

const router = new VueRouter({
  routes
})

// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // 当在登录页面时，放行，继续后续代码
  if (to.path === '/login') {
    return next()
  }
  // 当页面不在登陆页面时,根据是否有token值来判断，若不存在token值，就直接跳转到登陆页面
  const tokenStr = window.sessionStorage.getItem('token')
  if (!tokenStr) {
    return next('/login')
  } else {
    next()
  }
})

export default router
